package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.OrderService;
import com.seal.yoyoshop.service.impl.GoodsServiceImpl;
import com.seal.yoyoshop.service.impl.OrderServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DeleteGoodsServlet", value = "/index/delete")
public class DeleteGoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null) {//session失效
            response.getWriter().append("login");
        } else {
            Order order = (Order) session.getAttribute("order");
            String goodsIdstr = request.getParameter("goodid");
            int goodsId = Integer.parseInt(goodsIdstr);
            Goods goods = goodsService.getGoodById(goodsId);
            boolean flag = orderService.DeleteGoodsToOrder(goods,order);
            if(!flag){
                session.removeAttribute("order");
            }
            response.getWriter().append("ok");
        }
    }
}
